//
//  SetCounddownViewController.m
//  HaoHint
//
//  Created by winter on 16/5/23.
//  Copyright © 2016年 wd. All rights reserved.
//

#import "SetCountdownViewController.h"

#import "SetInfoViewController.h"

#import "SetCountdownTitleCell.h"
#import "SetCountdownTimeCell.h"
#import "SetCountdownSoundCell.h"
#import "SetCountdownRemindCell.h"
#import "SetTimeTableViewCell.h"

@interface SetCountdownViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) SetCountdownTitleCellModel *titleModel;
@property (nonatomic, strong) SetCountdownTitleCellModel *actionModel;

@property (nonatomic, strong) SetCountdownTimeCellModel *timeModel;

@property (nonatomic, strong) SetCountdownRemindCellModel *remindModel;
@property (nonatomic, strong) SetCountdownRemindCellModel *secondRemindModel;

@property (nonatomic, strong) SetCountdownSoundCellModel *soundModel;
@property (nonatomic, strong) SetCountdownSoundCellModel *secondSoundModel;

/**  */
@property (nonatomic, getter=isHasValue) BOOL hasValue;
@end

@implementation SetCountdownViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"添加计时器";
    
    [self setupNavBarItem];
    [self setupTableView];
}

- (void)setCountdownModel:(CountdownModel *)countdownModel
{
    self.hasValue = countdownModel ? YES : NO;
    if (!countdownModel) {
        countdownModel = [[CountdownModel alloc] init];
    }
    _countdownModel = countdownModel;
    
    [self setupData];
}

- (void)setupNavBarItem
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(navigationBarAction:)];
    leftItem.tag = 100;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(navigationBarAction:)];
    rightItem.tag = 101;

    [self.navigationItem setRightBarButtonItem:rightItem];
    [self.navigationItem setLeftBarButtonItem:leftItem];
}

- (void)navigationBarAction:(UIBarButtonItem *)sender
{
    if (101 == sender.tag) {
        
        self.countdownModel.timeTitle = self.titleModel.cellTitle;
//        self.countdownModel.alertBody = self.actionModel.cellTitle;
        
        self.countdownModel.countdownTime = self.timeModel.timeString;
        
        self.countdownModel.preRemind = self.remindModel.remindStyle > 0;
        self.countdownModel.remindEarlyType = (NSInteger)self.remindModel.remindStyle;
        
        self.countdownModel.preSoundName = self.soundModel.soundName;
        self.countdownModel.soundName = self.secondSoundModel.soundName;
        
        if (self.setCountdownBlock) {
            self.setCountdownBlock(self.countdownModel);
        }
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)setupTableView
{
    [self.tableView registerNib:[UINib nibWithNibName:@"SetCountdownTitleCell" bundle:nil] forCellReuseIdentifier:@"titleCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SetCountdownTimeCell" bundle:nil] forCellReuseIdentifier:@"timeCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SetCountdownRemindCell" bundle:nil] forCellReuseIdentifier:@"remindCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SetCountdownSoundCell" bundle:nil] forCellReuseIdentifier:@"soundCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SetTimeTableViewCell" bundle:nil] forCellReuseIdentifier:@"setTimeCell"];
}

- (void)setupData
{
    self.titleModel = [[SetCountdownTitleCellModel alloc] init];
    self.titleModel.placerholder = @"标题";
    self.titleModel.cellTitle = self.isHasValue ? self.countdownModel.timeTitle : @"计时器";
    self.actionModel = [[SetCountdownTitleCellModel alloc] init];
    self.actionModel.placerholder = @"事件";
    self.actionModel.cellTitle = self.isHasValue ? self.countdownModel.alertBody : nil;
    
    self.timeModel = [[SetCountdownTimeCellModel alloc] init];
    self.timeModel.timeSecond = self.isHasValue ? self.countdownModel.timeSecond : 60;
    self.timeModel.editTime = NO;
    
    self.remindModel = [[SetCountdownRemindCellModel alloc] init];
    self.remindModel.secondRemind = self.countdownModel.isPreRemind;
    self.remindModel.remindStyle = self.isHasValue ? self.countdownModel.remindEarlyType : RemindStyleNone;
    self.secondRemindModel = [[SetCountdownRemindCellModel alloc] init];
    self.secondRemindModel.secondRemind = YES;
    self.secondRemindModel.remindStyle = RemindStyleNow;
    
    self.soundModel = [[SetCountdownSoundCellModel alloc] init];
    self.soundModel.secondSound = NO;
    self.soundModel.soundName =  self.isHasValue ? self.countdownModel.preSoundName :@"系统默认";
    self.secondSoundModel = [[SetCountdownSoundCellModel alloc] init];
    self.secondSoundModel.secondSound = YES;
    self.secondSoundModel.soundName =  self.isHasValue ? self.countdownModel.soundName :@"系统默认";
}

#pragma mark - tableView delegate & datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }
    else if (section == 1) {
        if (self.timeModel.isEditTime) {
            return 2;
        }
        return 1;
    }
    else if (section == 2) {
        return 2;
    }
    else  {
        return 2;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        SetCountdownTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"titleCell" forIndexPath:indexPath];
        if (0 == indexPath.row) {
            cell.titleModel = self.titleModel;
        }
        else {
            cell.titleModel = self.actionModel;
        }
        return cell;
    }
    else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            SetCountdownTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"timeCell" forIndexPath:indexPath];
            cell.timeModel = self.timeModel;
            return cell;
        }
        else {
            SetTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"setTimeCell" forIndexPath:indexPath];
            cell.setTimeBlock = ^(NSInteger countDownDuration){
                self.timeModel.timeSecond = countDownDuration;
                [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationNone];
            };
            return cell;
        }
    }
    else if (indexPath.section == 2) {
        SetCountdownRemindCell *cell = [tableView dequeueReusableCellWithIdentifier:@"remindCell" forIndexPath:indexPath];
        if (0 == indexPath.row) {
            cell.remindModel = self.remindModel;
        }
        else {
            cell.remindModel = self.secondRemindModel;
        }
        return cell;
    }
    else if (indexPath.section == 3) {
        SetCountdownSoundCell *cell = [tableView dequeueReusableCellWithIdentifier:@"soundCell" forIndexPath:indexPath];
        if (0 == indexPath.row) {
            cell.soundModel = self.soundModel;
        }
        else {
            cell.soundModel = self.secondSoundModel;
        }
        return cell;
    }
    else if (indexPath.section == 4) {
        return nil;
    }
    else {
        return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        
    }
    else if (indexPath.section == 1) {
        self.timeModel.editTime = !self.timeModel.isEditTime;
        [tableView reloadData];
    }
    else  {
        
        if (indexPath.section == 2 && indexPath.row == 1) {
            return;
        }
        
        SetInfoViewController *vc = [[SetInfoViewController alloc] initWithNibName:@"SetInfoViewController" bundle:nil];
        if (indexPath.section == 2) {
            vc.type = SetInfoViewType_Remind;
            vc.title = @"提醒";
        }
        else {
            vc.type = SetInfoViewType_Sound;
            vc.title = @"铃声";
        }
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        [self presentViewController:nav animated:YES completion:nil];
        
        
        vc.setInfoBlock = ^(NSString *blockString){
            if (indexPath.section == 2) {
                SetCountdownRemindCell *remindCell = [tableView cellForRowAtIndexPath:indexPath];
                self.remindModel.secondRemind = NO;
                self.remindModel.remindStyle = [blockString integerValue];
                remindCell.remindModel = self.remindModel;
            }
            else {
                SetCountdownSoundCell *soundCell = [tableView cellForRowAtIndexPath:indexPath];
                if (indexPath.row == 0) {
                    self.soundModel.secondSound = NO;
                    self.soundModel.soundName = blockString;
                    soundCell.soundModel = self.soundModel;
                }
                else {
                    self.secondSoundModel.secondSound = YES;
                    self.secondSoundModel.soundName = blockString;
                    soundCell.soundModel = self.secondSoundModel;
                }
            }
        };
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && indexPath.row == 1) {
        return iOS7 ? 160 : 216;
    }
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return 10;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}
@end
